package com.revai.engine.common.api;

import java.util.HashMap;
import java.util.Map;

public class CustomResult {
  private Map<String, Object> properties = new HashMap<>();

  public CustomResult() {
    this.setError_code(ResultCode.SUCCESS);
  }

  public CustomResult(ResultCode resultCode) {
    this.properties.put("error_code", resultCode.getCode());
  }

  public CustomResult(String error_msg) {
    this.setError_code(ResultCode.SUCCESS);
    this.properties.put("error_msg", error_msg);
  }

  public CustomResult(ResultCode resultCode, String error_msg) {
    this.properties.put("error_code", resultCode.getCode());
    this.properties.put("error_msg", error_msg);
  }

  public CustomResult(Map<String, Object> props) {
    this.setError_code(ResultCode.SUCCESS);
    props.keySet().forEach(key -> {
      this.properties.put(key, props.get(key));
    });
  }

  public CustomResult setError_code(ResultCode resultCode) {
    properties.put("error_code", resultCode.getCode());
    return this;
  }

  public CustomResult setError_msg(String error_msg) {
    properties.put("error_msg", error_msg);
    return this;
  }

  public CustomResult setData(Object data) {
    properties.put("data", data);
    return this;
  }

  public CustomResult setProperty(String key, Object value) {
    this.properties.put(key, value);
    return this;
  }

  public CustomResult setProperties(Map<String, Object> props) {
    props.keySet().forEach(key -> {
      this.properties.put(key, props.get(key));
    });
    return this;
  }

  public CustomResult success(Object data) {
    properties.put("error_msg", "ok");
    properties.put("data", data);
    return this;
  }

  public CustomResult failed() {
    this.setError_code(ResultCode.FAILED);
    this.setError_msg("error");
    return this;
  }

  public CustomResult failed(Object data) {
    this.setError_code(ResultCode.FAILED);
    this.setError_msg(data != null ? data.toString() : "error");
    return this;
  }

  public Object getProperty(String key) {
    return this.properties.get(key);
  }

  public Map<String, Object> getResult() {
    return this.properties;
  }
}
